package com.tutor.lzm.function.teacher.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewStub;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.activity.TeacherDetailsAct;
import com.tutor.lzm.adapter.TeacherAdapter;
import com.tutor.lzm.app.Constant;
import com.tutor.lzm.bean.TeacherRecommendBean;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TeacherCollectListRequest;
import com.tutor.lzm.http.response.TeacherListResponse;
import com.tutor.syslib.Logs;
import com.tutor.syslib.pulltorefresh.PullToRefreshBase;
import com.tutor.syslib.pulltorefresh.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzgpc on 2017-9-24.
 * 我的收藏
 */
public class CollectionTeacherAct extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView> {
    private PullToRefreshListView pull_refresh_list;
    ListView listView = null;
    private TeacherAdapter adapter;
    private List<TeacherRecommendBean> teacherList = new ArrayList<>();
    int position, listViemItemTop;//纪录listVew得位置

    public static void start(Context context) {
        Intent intent = new Intent(context, CollectionTeacherAct.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_collection_teacher);
        initTitle("我的收藏");
        initListView();
        initData();
    }


    private void initData() {

        getCollectionTeacherList(false);
    }


    /**
     * 初始化可滑动删除litview
     */
    private void initListView() {
        pull_refresh_list = (PullToRefreshListView) findViewById(R.id.collection_listView);
        pull_refresh_list.setPullRefreshEnabled(true);
        pull_refresh_list.setPullLoadEnabled(true);
        pull_refresh_list.setOnRefreshListener(this);
        listView = pull_refresh_list.getRefreshableView();
        listView.setHeaderDividersEnabled(false);
        listView.setFooterDividersEnabled(false);
        listView.setDividerHeight(0);
        adapter = new TeacherAdapter(this, teacherList);
        listView.setAdapter(adapter);
        ViewStub mViewStub = (ViewStub) findViewById(R.id.emptyView);
        listView.setEmptyView(mViewStub);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < listView.getHeaderViewsCount())
                    return;
                TeacherRecommendBean bean = teacherList.get(position);
                String teacherId = bean.teacherId;
                TeacherDetailsAct.start(CollectionTeacherAct.this, teacherId);
            }
        });

        //这里不用这个headView 直接在act中

        pull_refresh_list.doPullRefreshing(false, 0);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    position = listView.getFirstVisiblePosition();
                    View itemView = listView.getChildAt(0);
                    listViemItemTop = (itemView == null) ? 0 : itemView.getTop();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                //如果卡顿得话 逻辑放在上面 if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE)
//                if (titleView == null)
//                    return;
//                if (firstVisibleItem >= listView.getHeaderViewsCount())
//                    titleView.setVisibility(View.VISIBLE);
//                else
//                    titleView.setVisibility(View.GONE);
            }
        });
        Constant.setPullFormartRefreshTime(pull_refresh_list);
    }


    /**
     * 删除
     */
    private void delete() {

    }


    /**
     * 加载位置坐标
     */
    private int currentPage = 0;


    /**
     * 加载收藏老师
     *
     * @param isClear
     */
    private void getCollectionTeacherList(final boolean isClear) {

        if (isClear) {
            currentPage = 0;
        }

        final TeacherCollectListRequest request = new TeacherCollectListRequest();
        request.setUserId(AppSession.getInstance().getUserId());
        request.setStart(currentPage);
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                Logs.e("mainfragment", "response");
                TeacherListResponse teacherListResponse = (TeacherListResponse) response;
                List<TeacherRecommendBean> adResponse = teacherListResponse.teacherList;

                if (adResponse != null && adResponse.size() != 0) {
                    if (isClear) {
                        teacherList.clear();
                    }
                    currentPage++;
                    teacherList.addAll(adResponse);
                    adapter.notifyDataSetChanged();
                }

                pull_refresh_list.onPullUpRefreshComplete();
                pull_refresh_list.onPullDownRefreshComplete();
                Constant.setPullFormartRefreshTime(pull_refresh_list);
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);
                pull_refresh_list.onPullUpRefreshComplete();
                pull_refresh_list.onPullDownRefreshComplete();
                Constant.setPullFormartRefreshTime(pull_refresh_list);
            }
        });

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        getCollectionTeacherList(true);

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

    }
}
